package snack;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Game extends JPanel implements KeyListener, ActionListener {
    int[][]arr={{0,1},{1,0},{0,-1},{-1,0}};
    Set<Integer>set=new HashSet<>();
    Timer timer1=new Timer(100,this);
    int targe=0;
    int dec=0;
    boolean gameover=false;
    Point food=new Point(5,9);
    List<Point> list=new ArrayList<>();
    boolean isok=false;
    public Game() {
        init();
        setFocusable(true);
        addKeyListener(this);
    }
    public void init(){
        isok=false;
        targe=0;
        dec=0;
        list=new ArrayList<>();
        food=new Point(5,9);
        set=new HashSet<>();
        timer1.setActionCommand("1");
        timer1.start();
        list.add(new Point(0,2));list.add(new Point(1,2));list.add(new Point(2,2));
        set.add(48);set.add(49);set.add(50);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);
        g.fillRect(10,10,490,490);

        Data.food.paintIcon(this,g,food.x*21+10,
                food.y*21+10);
        if(gameover)
        {
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏结束!得分:"+(list.size()-3),100,200);
            init();
            isok=true;
        }
        ImageIcon[]head={Data.right,Data.down,Data.left,Data.up};
        head[targe].paintIcon(this,g,list.get(list.size()-1).x*21+10,
                list.get(list.size()-1).y*21+10);
        for(int i=0;i<list.size()-1;i++)
            Data.body.paintIcon(this,g,list.get(i).x*21+10,
                    list.get(i).y*21+10);

        if(!isok)
        {
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按空格开始游戏!",100,200);
        }
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑",Font.BOLD,40));
        g.drawString(list.size()-3+"",430,50);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (isok&&!gameover){
            if(e.getActionCommand().equals("1"))
            {
                Point point=list.get(list.size()-1);
                Point point1=new Point((point.x+arr[targe][1]+23)%23,
                        (point.y+arr[targe][0]+23)%23);
                int num=point1.x+point1.y*24;
                if(set.contains(num)) {
                    gameover=true;
                }
                set.add(num);
                list.add(point1);
                if(point1.x==food.x&&point1.y==food.y)
                {
                    while (set.contains(food.x+food.y*24))
                    {
                        food.x=(int) (Math.random() * 23);
                        food.y=(int) (Math.random() * 23);
                    }
                }
                else {
                    set.remove(list.get(0).x+list.get(0).y*24);
                    list.remove(0);
                }
                dec=targe;
                repaint();
            }
        }
    }
    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==32) {
            isok = !isok;
            gameover=false;
            repaint();
        }
        if(e.getKeyCode()==KeyEvent.VK_UP&&dec!=1)targe=3;
        if(e.getKeyCode()==KeyEvent.VK_DOWN&&dec!=3)targe=1;
        if(e.getKeyCode()==KeyEvent.VK_LEFT&&dec!=0)targe=2;
        if(e.getKeyCode()==KeyEvent.VK_RIGHT&&dec!=2)targe=0;
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {

    }

}